package router

import (
	"github.com/gin-gonic/gin"
	"go-sec-code/database"
	"net/http"
)

type UserListHandler struct {
}

func NewGetUserListHandler() *UserListHandler {
	return &UserListHandler{}
}

func (handler *UserListHandler) RegisterRoutes(r *gin.RouterGroup) {
	r.GET("/user/list", handler.GetUserList)
}

func (handler *UserListHandler) GetUserList(ctx *gin.Context)  {
	name := ctx.Query("name")
	field := ctx.Query("field")
	//pageNo, _ := strconv.Atoi(ctx.Query("pageNo"))
	//pageSize, _ := strconv.Atoi(ctx.Query("pageSize"))

	//mysqlUser := database.NewMysqlUser()
	//db, err := mysqlUser.ConnectDB()
	//if err != nil {
	//	ctx.String(http.StatusOK, err.Error())
	//	return
	//}
	//users := mysqlUser.SelectByName(db, name, field)
	//users := mysqlUser.SelectLimit(db, pageSize, (pageNo - 1) * pageSize)

	//xormUser := database.NewXormUser()
	//engine := xormUser.GetEngine()
	//users := xormUser.SelectUsersByName(engine, name, field)
	//users := xormUser.SelectUsersLimit(engine)
	//users := xormUser.SelectUsersIn(engine, []int{1,2,4})

	gormUser := database.NewGormUser()
	db := gormUser.GetDB()
	users := gormUser.SelectListByName(db, name, field)
	//users := gormUser.SelectIn(db, []int{1, 2, 4})
	//users := gormUser.SelectLikeByName(db, name)
	//users := gormUser.SelectPage(db, 2, 0)

	ctx.JSON(http.StatusOK, users)
}